import java.util.Arrays;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-28
 * Time: 14:40
 */
public class MyStack {

    public int[] elem;
    public int usedSize;

    public MyStack(){
        this.elem = new int[10];
    }

    //判断栈是否满
    public boolean isFull(){
        if (this.usedSize == this.elem.length) {
            return true;
        }
        return false;
    }

    //给栈添加数据
    public void push(int item){
        if (isFull()) {
            //扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        //新数据存放到数组最后位置
        this.elem[usedSize] = item;
        usedSize++;
    }

    //判断栈是否为空
    public boolean empty(){
        if (this.usedSize == 0) {
            return true;
        }
        return false;
    }

    //将栈顶的数据弹出
    public int pop() throws RuntimeException{
        if (empty()) {
            throw new RuntimeException("栈为空~");
        }
        int val = this.elem[this.usedSize - 1];
        this.usedSize--;
        return val;
    }

    //查看栈顶数据
    public int peek() throws RuntimeException{
        if (empty()) {
            throw new RuntimeException("栈为空~");
        }
        int value = this.elem[this.usedSize-1];
        return value;
    }
}
